package com.baizhi.controller;

import com.baizhi.entity.Section;
import com.baizhi.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SectionController {
    @Autowired
    private SectionService sectionService;

    @GetMapping("/sections")
    public Map show(String search, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit) {
        List<Section> all = sectionService.selectAll(search,page, limit);
        Integer count = sectionService.count(search);
        Map map = new HashMap();
        map.put("data", all);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }

    @PostMapping("/sections")
    public Map add(@RequestBody Section s) {
        Map map = new HashMap();
        try {
            sectionService.insert(s);
            map.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("error", "添加失败");
        }
        return map;


    }

    @DeleteMapping("/sections/{id}")

    public Map delete(@PathVariable("id") Integer id) {
        System.out.println("88888888888888888888888888888");
        Map map = new HashMap();

        try {
            sectionService.deleteById(id);
            System.out.println("999999999999999999999999999999999999");
            map.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("6666666666666666666666666666666666");
            map.put("error", "删除失败");
        }
        return map;

    }
    @PutMapping("/sections")
    public Map update(@RequestBody Section s){
        Map map=new HashMap();
        try {
            sectionService.update(s);
            map.put("status","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("error","更新失败");
        }
        return map;
    }
    @DeleteMapping("/sections")
    public Map removeAny( @RequestBody List<Integer>ids){
        System.out.println("11111111111111111111111111111111111111");
        Map map=new HashMap();
        try {
            sectionService.deleteAny(ids);
            System.out.println("22222222222222222222222222222222");
            map.put("status","success");
            System.out.println("33333333333333333333333333333333333333");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("error","删除失败");
        }
        return map;
    }

}